package com.example.activitysign.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.activitysign.entity.ActivityUserTaskRecord;
import com.example.activitysign.mapper.ActivityMapper;
import com.example.activitysign.mapper.ActivityTaskItemMapper;
import com.example.activitysign.mapper.ActivityUserJoinMapper;
import com.example.activitysign.mapper.ActivityUserTaskRecordMapper;
import com.example.activitysign.pages.PageResult;
import com.example.activitysign.pages.PageValidate;
import com.example.activitysign.service.IActivityUserTaskRecordService;
import com.example.activitysign.validate.ActivityUserTaskRecordCreateValidate;
import com.example.activitysign.validate.ActivityUserTaskRecordSearchValidate;
import com.example.activitysign.validate.ActivityUserTaskRecordUpdateValidate;
import com.example.activitysign.vo.activity.ActivityUserTaskRecordListedVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 用户任务记录实现类
 *
 * @author fei
 */
@Slf4j
@Service
public class ActivityUserTaskRecordServiceImpl extends ServiceImpl<ActivityUserTaskRecordMapper, ActivityUserTaskRecord> implements IActivityUserTaskRecordService {
    public static SimpleDateFormat format1 = new SimpleDateFormat(
            "yyyyMMdd HH:mm:ss");
    @Resource
    private ActivityUserTaskRecordMapper activityUserTaskRecordMapper;
    @Resource
    private ActivityMapper activityMapper;
    @Resource
    private ActivityTaskItemMapper activityTaskItemMapper;
    @Resource
    private ActivityUserJoinMapper activityUserJoinMapper;


    /**
     * 用户任务记录列表
     *
     * @param pageValidate   分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<ActivityUserTaskRecordListedVo>
     * @author fei
     */
    @Override
    public PageResult<ActivityUserTaskRecordListedVo> list(PageValidate pageValidate, ActivityUserTaskRecordSearchValidate searchValidate) {
        Page<ActivityUserTaskRecord> page = new Page<>(pageValidate.getPageNo(), pageValidate.getPageSize());
        IPage<ActivityUserTaskRecordListedVo> list = activityUserTaskRecordMapper.list(page, searchValidate);
        return PageResult.iPageHandle(list.getTotal(), list.getCurrent(), list.getSize(), list.getRecords());
    }

    /**
     * 用户任务记录详情
     *
     * @param id 主键参数
     * @return ActivityUserTaskRecord
     * @author fei
     */
    @Override
    public ActivityUserTaskRecordListedVo detail(Long id) {
        return activityUserTaskRecordMapper.getDetailById(id);
    }

    /**
     * 用户任务记录新增
     *
     * @param createValidate 参数
     * @author fei
     */
    @Override
    public void add(ActivityUserTaskRecordCreateValidate createValidate) {
        ActivityUserTaskRecord model = new ActivityUserTaskRecord();
        BeanUtils.copyProperties(createValidate, model);
        model.setCreateTime(new Date());
        model.setUpdateTime(new Date());
        activityUserTaskRecordMapper.insert(model);
    }

    /**
     * 用户任务记录编辑
     *
     * @param updateValidate 参数
     * @author fei
     */
    @Override
    public void edit(ActivityUserTaskRecordUpdateValidate updateValidate) {
        ActivityUserTaskRecord model = activityUserTaskRecordMapper.selectById(updateValidate.getId());
        Assert.notNull(model, "数据不存在!");
        BeanUtils.copyProperties(updateValidate, model);
        model.setUpdateTime(new Date());
        activityUserTaskRecordMapper.updateById(model);
    }

    /**
     * 用户任务记录删除
     *
     * @param id 主键ID
     * @author fei
     */
    @Override
    public void del(Long id) {
        int i = activityUserTaskRecordMapper.deleteById(id);
        Assert.isTrue(i > 0, "数据不存在!");
    }

}

